#include "Sqlist.h"


Status initSqlist(sqlist *L)
{
    L->elem = (sqlelemtype*)malloc(sizeof(sqlelemtype)*SqlistMaxSzie);//申请存放链表的地址
    if (!L->elem)
    {
        return OVERFLOW;
    }
    L->length = 0;
    return OK;
}

Status DestorySqlist(sqlist *L)
{
    free(L);
    if (!L->elem)
    {
        return INFEASIBLE;
    }  
    return OK;
}

Status ClearSqlist(sqlist *L)
{
    //清除内容，不释放地址
    L->length = 0;
    return OK;
}

Status SqListDelete(sqlist *L,int i)//0<=i<length
{
    if (i < 0 || i >= L->length)
    {
        return OVERFLOW;
    }
    for (int m = 0; m < L->length - i; m++)
    {
        L->elem[m + i] = L->elem[m + i +1];
    }
    L->length = L->length - 1;
}

Status SqListInsert(sqlist *L,int i,sqlelemtype e)//0<=i<=length
{
    if (i < 0 || i > L->length)
    {
        return OVERFLOW;
    }
    if (L->length + 1 > SqlistMaxSzie)
    {
        return INFEASIBLE;
    }
    for (int m = 0; m < (L->length - i); m++)
    {
        L->elem[L->length-m] = L->elem[L->length-1-m];
    }
    L->elem[i] = e;
    L->length=L->length + 1;
    return OK;
}

bool SqIsEmpty(sqlist *L)
{
    if (L->length > 0)
    {
        return TRUE;
    }
    else
    {
        return FALSE;

    }
}

int SqListLength(sqlist *L)
{   
    if (L == NULL)
    {
        return 0;
    }
   return L->length; 
}

int LocateSql(sqlist *L,sqlelemtype e)
{
    for (int  i = 0; i < L->length; i++)
    {
        if (e = L->elem[i])
        {
            return i;
        }  
    }
    return 0;  
}


Status GetSqElem(sqlist *L,int i,sqlelemtype *e)
{
    
    if (i > L->length || i < 0)
    {
        return OVERFLOW;
    }    
    e = L->elem + sizeof(sqlelemtype)*i;//从i从0开始
    return OK;
}
//***************************线性表的链式结构实现***********************************
Status InitList(LinkList *L)
{
    (*L) = (LinkList)malloc(sizeof(LinkList));
    (*L)->next = NULL;
    
    return OK;
}

Status LinkGetElem(LinkList L,int i,sqlelemtype *e)
{
    if (L->next==NULL)
    {
        return ERROR;
    }
    LNode *p=L->next;
    int j=1;
    while(p && j<i)
    {
        p=p->next;
        j++;
    }
    if(!p ||j>i) return ERROR;//i从1开始计算
    *e=p->data;
    return OK;
}

LNode* LinkLocate(LinkList L,sqlelemtype e)
{
    LNode *p;
    p=L->next;
    while (p && (p->data!=e))
    {
        p=p->next;
    }
    return p;
}

Status LinkInsert(LinkList L,int i,LNode* x)
{
    //首元结点下标号为1,无头结点
    LNode* p = L;
    int j = 1;
    while (p && j<i)
    {
        p=p->next;
        j++;
    }
    if (!p || j>i) return ERROR;
    x->next = p->next;
    p->next = x;
    return OK;
}

Status LinkDelete(LinkList L,int i)
{
    LNode *p = L;
    int j = 1;
    while (p->next && j <i)//i从1开始
    {
        p=p->next;
    }
    if (!p->next || j>i) return ERROR;  
    p->next=p->next->next;
    free(p);
}


Status LinkIsEmpty(LinkList L)
{
    if (!L->next) return 1;
    else  return  0;
}

Status DestortLink(LinkList *L)
{
    
    while ((*L))
    {
       LNode *p = *L;
        (*L) = (*L)->next;
        free(p);
    }
    return OK;
}

Status LinkClear(LinkList L)
{
    LNode *p = L->next;
    LNode *pfree;
    while (p)
    {
        pfree = p;
        p= p->next;
        free(pfree); 
    }
}

int Linkcount(LinkList L)
{
    LNode *p;
    p = L->next;
    int j=0;
    while (p)
    {
        p=p->next;
        j++;
    }
    return j;
}


void LinkCreate_H(LinkList L, int num)//从an---a1依次输入
{
    int data ;
    for (int i = 0; i < num; i++)
    {
        scanf(" %d", &data);
        LNode* p =(LNode*)malloc(sizeof(LNode));
        p->data = data;
        p->next =L->next;
        L->next = p;    
    }
    
}
void LinkCreate_V(LinkList L, int num)
{
    int data ;
    LNode *r = L;
    for (int i = 0; i < num; i++)
    {
        scanf(" %d", &data);
        LNode* p =(LNode*)malloc(sizeof(LNode));
        p->data = data;
        p->next = NULL;

        r->next = p;
        r = p;
           
    }


}

LinkList* LinkConnect(LinkList *Ta,LinkList *Tb)
{
     LNode *p;
    p = (*Ta)->next;
     (*Ta)->next = (*Tb)->next->next;
    free((*Tb)->next);
 return *Tb; 
}
//*************************************双向链表***********************************************************//////最好用C++s实现双向链表，p->next->prior报错
void DouLinkListInit(DouLinkList *L)
{
    (*L)=(DouLinkList*)malloc(sizeof(DouLinkList));
    (*L)->prior = NULL;
    (*L)->next = NULL;
}
DouLNode* GetElem_Dul(DouLinkList L,int i)
{
    DouLNode *p =L;
    int j = 1;
    while (p->next && j < i)
    {
        p = p->next;
        j++;
    }
    if (i==j) 
    {
        return p;
    }
}


void DouLinkListinsert(DouLinkList *L, int num,sqlelemtype e)
{
    DouLinkList p;
    DouLNode *p1;
    p = GetElem_Dul(L,num);
    p1->data = e;
    p1->next = p->next;
    p1->prior = p;
    p->next = p1;
    //(p->next)->prior = p1;

}

void CreatDL_H(DouLinkList L, int length)//头插法
{
    for (int i = 0; i < length; i++)
    {
        sqlelemtype data;
        DouLNode *p1;
        scanf("%d",&data);
        DouLNode *p = (DouLNode*)malloc(sizeof(DouLNode));
        p->data = data;
        //L->next->prior = p;
        if (i == 0)
        {
            p->next = L->next;
            p->prior = L;     
            L->next = p;
            L->prior = p;
        }
        else
        {
            p->next = L->next;
            p->prior = L; 
            L->next = p;
            p1->prior = p;

        }
    }
    

}
void CreatDL_V(DouLinkList L, int length)
{
    for (int i = 0; i < length; i++)
    {
        DouLNode *p1;
        sqlelemtype data;
        scanf("%d",&data);
        DouLNode *p = (DouLNode*)malloc(sizeof(DouLNode));
        p->data = data;
        //L->prior->next = p; 
        p->next = L;        
        p->prior = L->prior;
        L->prior = p;

        if (i == 0)
        {
            p->next = L->next;
            p->prior = L;     
            L->next = p;
            L->prior = p;
            p1 = p;
        }
        else
        {
            p->next = L->next;
            p->prior = p1; 
            L->prior = p;
            p1 = p;

        }
        
    }
}